package com.leetcode;

public class Atoi
{
	public static void main(String[] args) throws Exception
	{
		Atoi a = new Atoi();
		System.out.println(a.atoi("1256")+"");
	}

	public long atoi(String str) throws Exception
	{
		boolean negative = false;
		long value = 0;
		if (str == null || str.equals(""))
		{
			throw new Exception("null string or the string has no character!");
		}
		for (int i = 0; i < str.length(); i++)
		{
			OutTxt.Write(str.length()+"");
			if (i == 0 && (str.charAt(0) == '-' || str.charAt(0) == '+'))
			{
				if (str.charAt(0) == '-')
				{
					negative = true;
				}
			}
			else
			{
				if (str.charAt(i) >= '0' && str.charAt(i)<='9' )
				{
					value = value * 10 + (str.charAt(i) - '0');
					if (value > Integer.MAX_VALUE)
					{
						throw new Exception("OUT OF INTEGER RANGE");
					}
				}
				else
				{
					throw new NumberFormatException("not an integer");
				}
			}
		}
		return negative == true ? value * -1 : value;
	}

}
